package com.lottery.ruok.lottrey.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.eventbusObject.ChargePush;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.IsPaymentFinishResquest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetPayTypeResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.IsPaymentFinishBean;
import com.lottery.ruok.lottrey.retrofitModule.response.PayPostResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;
import com.lottery.ruok.lottrey.utils.StringUtils;
import com.lottery.ruok.lottrey.utils.SystemUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ui.NavigateBar;
import utils.Utils;

/**
 * 作者：lm
 * <p>
 * 创建日期：2018/9/30$
 * <p>
 * 描述：
 */
public class ShanYinRechargeActivity extends BaseActivity {
    @BindView(R.id.order_id_txt)
    TextView order_id_txt;
    @BindView(R.id.amount_txt)
    TextView amount_txt;
    @BindView(R.id.qr_code_img)
    ImageView qr_code_img;
    @BindView(R.id.ali_pay_btn)
    TextView ali_pay_btn;
@BindView(R.id.navigateBar)
NavigateBar navigateBar;
    private PayPostResponse payPostResponse;
    private GetPayTypeResponse.GetPayItemsResponse itemData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shan_yin_recharge);
        ButterKnife.bind(this);
        initView();
    }

    private void initView(){
        payPostResponse = (PayPostResponse) getIntent().getSerializableExtra("payPostResponse");
        itemData=  (GetPayTypeResponse.GetPayItemsResponse) getIntent().getSerializableExtra("itemData");
        if (itemData != null) {
            if (StringUtils.isNotEmpty(itemData.getItem_name())){
                navigateBar.setCenterTitle(itemData.getItem_name());
            }
        }

        String orderId = payPostResponse.getOrdernum();
        String amount = payPostResponse.getAmount();

        order_id_txt.setText(orderId);

        SpannableStringBuilder sb = new SpannableStringBuilder(amount);
        sb.append("  元");
        sb.setSpan(new ForegroundColorSpan(Color.parseColor("#661DAC")),
                0, amount.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        amount_txt.setText(sb);
        Glide.with(this).load(payPostResponse.getCode_img_url()).into(qr_code_img);
    }

    public static void  gotoShanYinRecharge(Context context,PayPostResponse payPostResponse,GetPayTypeResponse.GetPayItemsResponse itemData){
        Intent intent = new Intent(context,ShanYinRechargeActivity.class);
        intent.putExtra("payPostResponse",payPostResponse);
        intent.putExtra("itemData",itemData);
        context.startActivity(intent);
    }


    @OnClick(R.id.ali_pay_btn)
    public void aLiPay(){
        qr_code_img.setVisibility(View.GONE);
        ali_pay_btn.setVisibility(View.GONE);
        if ( SystemUtil.checkAliPayInstalled(this)) {
            Utils.openBrowser("alipays://platformapi/startapp?saId=10000007&qrcode="+payPostResponse.getQrcode(),this);
        }else {
            Utils.openBrowser(payPostResponse.getQrcode(),this);
        }

    }

    @OnClick(R.id.submit_btn)
    public void submit(){
        RetrofitBase.getRetrofit().create(IsPaymentFinishResquest.class)
                .getResult(UserInfo.getToken(),payPostResponse.getOrdernum())
                .compose(RxHelper.<IsPaymentFinishBean>handleResult())
                .subscribe(new RxSubscribe<IsPaymentFinishBean>(this) {
                    @Override
                    protected void _onNext(IsPaymentFinishBean forgetPasswordResponse) {

if (forgetPasswordResponse.getData().getStatus()==1){
    Toast.makeText(ShanYinRechargeActivity.this,"支付成功",Toast.LENGTH_SHORT).show();
    EventBus.getDefault().post(new ChargePush());
}else if (forgetPasswordResponse.getData().getStatus()==0){
    Toast.makeText(ShanYinRechargeActivity.this,"稍后刷新页面\n若仍未到账联系客服",Toast.LENGTH_SHORT).show();
    EventBus.getDefault().post(new ChargePush());
}
finish();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }

                    @Override
                    protected boolean showDialog() {
                        return true;
                    }
                });
    }


    @OnClick(R.id.choose_other_txt)
    public void chooseOtherPay(){
        finish();
    }
}
